# 获取乐队的所有专辑及歌曲信息
@app.route('/get_all_album', methods=['GET', 'POST'])
def get_all_album():
if 'username' not in session:
return redirect(url_for('login'))  # 如果没有登录，重定向到登录页面

username = session['username']  # 获取当前用户名

# 获取乐队所有专辑信息
connection = get_db_connection()
with connection.cursor() as cursor:
cursor.execute("""
SELECT album_name, album_time, album_intro
FROM album_info
WHERE song_master = %s
""", (username,))
albums = cursor.fetchall()

# 获取每个专辑的歌曲信息
album_songs = {}
for album in albums:
album_name = album[0]
cursor.execute("""
SELECT song_name, song_writer, song_release
FROM song_info
WHERE song_belong = %s
""", (album_name,))
songs = cursor.fetchall()
album_songs[album_name] = songs

connection.close()

if request.method == 'POST':
# 添加歌曲信息
album_name = request.form['album_name']
song_name = request.form['song_name']
song_writer = request.form['song_writer']
song_release = request.form['song_release']

# 将歌曲信息插入数据库
connection = get_db_connection()
with connection.cursor() as cursor:
cursor.execute("""
INSERT INTO song_info (song_name, song_writer, song_release, song_belong)
VALUES (%s, %s, %s, %s)
""", (song_name, song_writer, song_release, album_name))
connection.commit()

connection.close()
flash('歌曲添加成功！')
return redirect(url_for('all_album_info'))


return render_template('get_all_album.html', albums=albums, album_songs=album_songs)